//
//  main.cpp
//  Mir_2
//
//  Created by 종헌 백 on 12. 1. 11..
//  Copyright (c) 2012년 backssi1@naver.com. All rights reserved.
//

#include <iostream>
#include <stdlib.h>
#include <ctime>

using namespace std;

int Compare_Number(int a[],int b[]);       //작성한 숫자와 정답 숫자와 일치하는 갯수 비교
void array_number ( int *a );

int main ()
{
    int PutNumber[7];               //입력 번호
    int AnswerNumber[7];            //정답 번호
    int n,i,j;
    int grade;
    
    srand((unsigned)time(NULL));
    
    cout<<"Welcome to Lotto Simulation Pogram!"<<endl<<endl;
    cout<<"1등은 6개 번호 일치 : 100억"<<endl;
    cout<<"2등은 5개 번호 일치,보너스 번호 일치 : 1억"<<endl;
    cout<<"3등은 5개 번호 일치 : 1천만"<<endl;
    cout<<"4등은 4개 번호 일치 : 100만"<<endl;
    cout<<"5등은 3개 번호 일치 : 10만"<<endl;
    cout<<"나머지는 꽝!! 다음기회에."<<endl<<endl<<endl;
    
    cout<<"Please Select the Rule."<<endl<<endl;
    cout<<"1.Read from the keyboard"<<endl;
    cout<<"2.Randomly generate"<<endl<<endl;
    
    do{
        cout<<"How type to select your number?( 1 ~ 2 ) : "<<endl;
        
        cin>>n;
    }while ( n != 1 && n != 2 );
    
    if ( n == 1 )                                                       //직접 입력 1번 선택
    {
        for ( i = 0 ; i < 7 ; i++ )                                 //6개번호 순서대로 입력 ( 1~45, 중복 없이 )
        { 
            if ( i == 6 )
            {
                cout<<"Input your number BONUS : ";
                cin>>PutNumber[i];
                
                for ( j = 0 ; j < 6 ; j++ )                          //보너스 번호 중복부분                      
                {
                    if ( PutNumber[i] == PutNumber[j] || PutNumber[i] < 1 || PutNumber[i] > 45 )
                    {
                        i--;
                        break;
                    }
                } 
                
            }
            
            else
            {
                cout<<"Input your number "<<i+1<<" : ";
                cin>>PutNumber[i];
                
                for ( j = 0 ; j < i ; j++ )                           
                {
                    if ( PutNumber[i] == PutNumber[j] ||  PutNumber[i] < 1 || PutNumber[i] > 45  )
                    {
                        i--;
                        break;
                    }
                }
            }
        }
    }
    
    
    
    else
    {
        for ( i = 0 ; i < 7 ; i++ )
        {
            PutNumber[i] = rand()%45+1;
            for ( j = 0 ; j < i ; j++ )
            {
                if ( PutNumber[i] == PutNumber[j] )
                    i--;
                break;
            }
        }
    }
    
    
for ( i = 0 ; i < 7 ; i++ )
    {
        AnswerNumber[i] = rand()%45+1;
        for ( j = 0 ; j < i ; j++ )
        {
            if ( AnswerNumber[i] == AnswerNumber[j] )
                i--;
            break;
        }
    }
    array_number ( PutNumber );
    array_number ( AnswerNumber );
    
    
    cout<<endl<<"   Your Numbers :";                         //입력,정답 숫자 출력
    for ( i = 0 ; i < 6 ; i++ )
    {
        cout<<"\t"<<PutNumber[i]<<"\t";
    }
    cout<<" , Bonus Number : "<<PutNumber[6]<<endl;
    
    cout<<"   Winning Numbers :";
    for ( i = 0 ; i < 6 ; i++ )
    {
        cout<<"\t"<<AnswerNumber[i]<<"\t";
    }
    cout<<" , Bonus Number : "<<AnswerNumber[6];
    
    
    grade = 8 - Compare_Number(PutNumber, AnswerNumber);             //당첨 비교 함수 사용
    
    if ( grade >= 6 )
        grade = 6;
    
    cout<<endl<<endl<<"   You're grade : "<<grade<<" 등 "<<endl<<endl;
    
    switch (grade) 
    {
        case 2:
            cout<<"Congratulation! You win the game! You received 100억!";
            break;
            
        case 1:
            cout<<"You received 1억";
            break;
            
        case 3:
            cout<<"You received 1천만";
            break;
            
        case 4:
            cout<<"You received 100만";
            break;
            
        case 5:
            cout<<"You received 10만";
            break;
        default:
            cout<<"You are lost! Can't received anything..";
            break;        
            
            
            break;
    }
    
    return 0;
}


int Compare_Number(int a[],int b[])
{
    int i,j;
    int point = 0;
    
    for ( i = 0 ; i < 6 ; i++ )
    {
        for ( j = 0 ; j < 6 ; j++ )
        {
            if ( a[i] == b[j] )
                point++;
        }
        
    }
    if ( (point == 5) && a[6] == b[6] )
    {
        point = point + 2;
    }
    
    return point;
}

void array_number ( int *a )
{
    int temp;
    int i,j;
    
    for ( i = 0 ; i < 6 ; i++ )
    {
        for ( j = 0 ; j < i ; j++ )
            if ( a[i] < a[j] )
            {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
    }
}